
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;   
import javax.mail.internet.MimeMultipart;
public class COM_email implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}
		String rest = null;
		boolean istrue=true;
		for(String s: extra){
			if(istrue){
				istrue = false;
			}else{
				if(rest !=null)
					rest = rest+" "+s.trim();
				else
					rest = s.trim();
			}
		
		//System.out.println(substring);
		}
		String tosend = "The per 1000 message email allocation has been depleted.";
		if(irc.messagesleft>0 && extra.length>0){
			irc.messagesleft--;
		
			tosend = "It was a failure.";
		 String to = extra[0];
		 try{
		  final String from = irc.emailfrom;
		  String host = irc.emailsmtpserver;
		  //String filename = "chat.zip";
		  String subject = sender+" from "+channel+" on the server "+irc.getServer()+ " sent this...";
		  String msgText1 = rest+"\n\n\nThe user has the hostname of: "+hostname;
		  final String password = irc.emailpass;
		  
		  // create some properties and get the default Session
		 
		  Properties props = System.getProperties();
		    props.put("mail.smtp.starttls.enable", "true"); // added this line
		    props.put("mail.smtp.host", host);
		    props.put("mail.smtp.user", from);
		    props.put("mail.smtp.password", password);
		    props.put("mail.smtp.port", irc.emailsmtpport);
		    props.put("mail.smtp.auth", "true");
//           Authenticator auth = new Authenticator() {
//
//               @Override
//               p PasswordAuthentication getPasswordAuthentication() {
//                   return new PasswordAuthentication(from, password);
//               }
//           };

		  Session session = Session.getInstance(props);

	
		      // create a message
		      MimeMessage msg = new MimeMessage(session);
		      msg.setFrom(new InternetAddress(from));
		      InternetAddress[] address = {new InternetAddress(to)};
		      msg.setRecipients(Message.RecipientType.TO, address);
		      msg.setSubject(subject);

		      // create and fill the first message part
		      MimeBodyPart mbp1 = new MimeBodyPart();
		      mbp1.setText(msgText1);

		      // create the second message part


		            // attach the file to the message
		        
		   

		      // create the Multipart and add its parts to it
		      Multipart mp = new MimeMultipart();
		      mp.addBodyPart(mbp1);

		      // add the Multipart to the message
		      msg.setContent(mp);

		      // set the Date: header
		      msg.setSentDate(new Date());
		      
		      // send the message
		      Transport transport = session.getTransport("smtp");
		      transport.connect (host, 587, from, password);
		      transport.sendMessage(msg, msg.getAllRecipients());
		      transport.close();    
		      tosend = "The email appears to be successfully sent! There are "+irc.messagesleft+" emails left.";
		  } 
		  catch (MessagingException mex) 
		  {
		      mex.printStackTrace();
		      Exception ex = null;
		      if ((ex = mex.getNextException()) != null) {
		    ex.printStackTrace();
		      }
		  }
		}
			
		
		
	if(extra.length==0){
		tosend= "Say something! Tou should use the format of email [to] [message]";
	}
		irc.sendNotice(sender,  tosend);
	}

	public String help() {
		return "Email - email [to] [message] Sends an email to the specified email address with the specified message";
		// TODO Auto-generated method stub

	}

	public String version() {
		return "Email version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "emailit, email";
	}

}
